<?php
// +----------------------------------------------------------------------
// | 公共登陆引用文件   D:20150826   M:501468940
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/func.inc.php';

// 
$password = md5(md5($_POST["password"]));
$passport = addslashes($_POST['passport']);


function login($passport, $password){

	if(p_match($passport)) return array('status'=>'fali','msg'=>'账号存在非法字符');
	
	$sql = "select m.dname,m.status,m.id,m.password,m.derror,m.errortime,m.dtype,m.account,m.member_auth,u.id as uid from pft_member m left join pft_distributor_union_SE u on m.id=u.fid where (m.account='{$passport}' or m.mobile='{$passport}') AND m.status in (0,3) limit 1";
	$GLOBALS['le']->query($sql);
	if(!($a_info=$GLOBALS['le']->fetch_assoc())) return array('status'=>'fali','msg'=>'账号不存在或已停用');
	
	if($a_info['password']!=$password){
		$str = ($a_info['derror']==5) ? ",errortime=now()":"";
		$upd = "update pft_member set derror=derror+1".$str." where id=".$a_info['id']." limit 1";
		$GLOBALS['le']->query($upd);
		return array('status'=>'fali','msg'=>'密码错误');
	}
	
	if($a_info['status'] == 3) return array('status'=>'fali','msg'=>'该账号未审核，请联系客服进行审核');
	if($a_info['derror']>5 && (strtotime("now")-strtotime($errortime))<(0*30))
		return array('status'=>'fali','msg'=>'登录错误次数超过5次!半小时内不可登录');
	// 登陆成功
	$_SESSION['alliance'] = $a_info['uid']; 
	$_SESSION['dname']    = $a_info['dname'];
	$_SESSION['dtype']    = $_SESSION['sdtype']  = $a_info['dtype'];
	$_SESSION['memberID'] = $_SESSION['sid']     = $a_info['id'];
	$_SESSION['saccount'] = $_SESSION['account'] = $a_info['account'];
	
	// 原散客
	if(ismobile($_SESSION['account'])) $_SESSION['dtype'] = $_SESSION['sdtype'] = 5 ;
	
	// 员工帐号
	if($a_info['dtype']==6){
		$_SESSION['qx'] = $a_info['member_auth'];
		$sel = "select parent_id from pft_member_relationship where son_id_type=2 and ship_type=1 and son_id=".$a_info['id']." limit 1";
		$GLOBALS['le']->query($sel);
		$GLOBALS['le']->fetch_assoc();
		$_SESSION['sid'] = $GLOBALS['le']->f("parent_id");
		
		$sel = "select dname,dtype,account from pft_member where id=".$_SESSION['sid']." limit 1";
		$GLOBALS['le']->query($sel);
		$GLOBALS['le']->fetch_assoc();
		$_SESSION['sdtype'] = $GLOBALS['le']->f("dtype");
		$_SESSION['sdname'] = $GLOBALS['le']->f("dname");
		// 价格新接口需要用到 account  因此员工账号获取父级账号
		$_SESSION['saccount'] = $GLOBALS['le']->f("account");
		
	}elseif($a_info['dtype']==0 || $a_info['dtype']==1){
		$sel="select m.id from pft_member_relationship r left join pft_member m on m.id=parent_id where son_id=".$a_info['id']." and m.dtype=7 and r.status=0 limit 1";
		$GLOBALS['le']->query($sel);
		if($GLOBALS['le']->fetch_assoc()) $_SESSION['group']=$GLOBALS['le']->f("id");
	}
	
	// 更新登陆信息
	$upd = "update pft_member set lasttime=now(),derror=0,errortime='' where id= ".$_SESSION['memberID']." limit 1";
	$GLOBALS['le']->query($upd);
	return array('status'=>'success');
}

